package org.leviatan.dataharbour.core.model.control;

import org.leviatan.dataharbour.core.model.DataEntity;
import org.leviatan.dataharbour.core.model.impl.DataEntityImpl;
import org.w3c.dom.Element;


public abstract class DataEntityHandler {
	
	public static String convertToXml(DataEntity xmlObj) {
		String xml = "";
		xml += "\n\t\t<id><![CDATA[" + xmlObj.getId() + "]]></id>";
		xml += "\n\t\t<name><![CDATA[" + xmlObj.getDataName() + "]]></name>";
		xml += "\n\t\t<type><![CDATA[" + xmlObj.getDataType() + "]]></type>";
		xml += "\n\t\t<harbour-request-id><![CDATA[" + xmlObj.getHarbourRequestId() + "]]></harbour-request-id>";
		xml += "\n\t\t<value><![CDATA[" + xmlObj.getDataValue() + "]]></value>";
		return xml;
	}
	
	public static DataEntity instantiateModel(Element e) {
		DataEntity de = new DataEntityImpl();
		String id = e.getElementsByTagName("id").item(0).getTextContent();
		String requestId = e.getElementsByTagName("harbour-request-id").item(0).getTextContent();
		String type = e.getElementsByTagName("type").item(0).getTextContent();
		String dataName = e.getElementsByTagName("name").item(0).getTextContent();
		String dataValue= e.getElementsByTagName("value").item(0).getTextContent();
		
		boolean internalType = false;
		if (type.equals("txt")) {
			internalType = true;
		}
		// bin type is internally = false
			
		de.setId(id);
		de.setHarbourRequestId(requestId);
		de.setDataType(internalType);
		de.setDataName(dataName);
		de.setDataValue(dataValue);
		
		return de;
		
	}
	

}
